/**
 * 
 */
package br.com.unifebe.tcc.componente.GUI;

import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.ParseException;

import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;

import br.com.unifebe.tcc.UTILS.Funcoes;


public class JMLMaskEdit extends JFormattedTextField {
	private JLabel msgLabel;
	private String atributo;
	
	/**
	 * 
	 */
	public JMLMaskEdit() {
		initComponentes("");
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param value
	 */
	public JMLMaskEdit(Object value) {
		super(value);
		// TODO Auto-generated constructor stub
	}

	
	

	/**
	 * @param format
	 */
	public JMLMaskEdit(String format) {
		super();
		initComponentes(format);
	}
	
	private void initComponentes(String format) {
		
//		this.setIgnoreRepaint(false);
		
		try {
			MaskFormatter formatter = new MaskFormatter(format);
			DefaultFormatterFactory factory =  new DefaultFormatterFactory(formatter,formatter);
			this.setFormatterFactory(factory);
			
		} catch (ParseException pe) {
			Funcoes.showMessageErro(pe.getMessage(), pe);
		}
		this.addKeyListener(new KeyListener(){
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode()==10) {
					((JComponent)findNextFocus()).grabFocus();
				}
			}
			@Override
			public void keyReleased(KeyEvent e) {
			}
			@Override
			public void keyTyped(KeyEvent e) {
			}
		});
		this.addFocusListener(new java.awt.event.FocusListener() {
			@Override
			public void focusGained(java.awt.event.FocusEvent e) {
//				JMLMaskEdit.this.setBackground(Parametros.getCorEditSelecionado());
//				JMLMaskEdit.this.setFont(Parametros.getFontEditSelecionado());
//				JMLMaskEdit.this.setForeground(Parametros.getCorFontEditSelecionado());
				if (JMLMaskEdit.this.msgLabel!=null) {
					JMLMaskEdit.this.msgLabel.setText(JMLMaskEdit.this.getToolTipText());
				}
				JMLMaskEdit.this.selectAll();

			}
			@Override
			public void focusLost(java.awt.event.FocusEvent e) {
//				JMLMaskEdit.this.setBackground(Parametros.getCorEdit());
//				JMLMaskEdit.this.setFont(Parametros.getFontEdit());
//				JMLMaskEdit.this.setForeground(Parametros.getCorFontEdit());
				if (JMLMaskEdit.this.msgLabel!=null) {
					JMLMaskEdit.this.msgLabel.setText("");
				}
			}
		});
	}
	
	/**
	 * @param formatter
	 */
	public JMLMaskEdit(AbstractFormatter formatter) {
		super(formatter);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param factory
	 */
	public JMLMaskEdit(AbstractFormatterFactory factory) {
		super(factory);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param factory
	 * @param currentValue
	 */
	public JMLMaskEdit(AbstractFormatterFactory factory, Object currentValue) {
		super(factory, currentValue);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @return the msgLabel
	 */
	public JLabel getMsgLabel() {
		return msgLabel;
	}

	/**
	 * @param msgLabel the msgLabel to set
	 */
	public void setMsgLabel(JLabel msgLabel) {
		this.msgLabel = msgLabel;
	}

	/**
	 * @return the atributo
	 */
	public String getAtributo() {
		return atributo;
	}

	/**
	 * @param atributo the atributo to set
	 */
	public void setAtributo(String atributo) {
		this.atributo = atributo;
	}

	public Component findNextFocus() {
        // Find focus owner
        Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
        Container root = c == null ? null : c.getFocusCycleRootAncestor();
    
        if (root != null) {
            FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
            Component nextFocus = policy.getComponentAfter(root, c);
            if (nextFocus == null) {
                nextFocus = policy.getDefaultComponent(root);
            }
            return nextFocus;
        }
        return null;
    }

	public Object getUserObject() {
		return null;
	}

	public boolean isEnabledControl() {
		return isEnabled();
	}

	public void reset() {
		
	}

	public void setEnabledControl(boolean b) {
		this.setEnabled( b );
	}

	public void setUserObject(Object object) {
		
	}

	
	public void initializing(boolean state) {
		// TODO Auto-generated method stub
		
	}}
